﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.ProcessRoute;
using PowerHub.MES.IServices.IProcessRoute;
using PowerHub.MES.DTO.ProcessRoute.QueryDto;
using PowerHub.MES.DTO.ProcessRoute.DTO;
using PowerHub.MES.IRepository.IProcessRoute;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PowerHub.MES.Services.ProcessRoute
{
    /// <summary>
    /// 工艺路线接口实现层
    /// </summary>
    public class ProcessRoute_CraftBillServices : BaseServices<ProcessRoute_CraftBill>, IProcessRoute_CraftBillServices
    {
        private IProcessRoute_CraftBillRepository _iProcessRoute_CraftBillRepository;
        private IProcessRoute_CraftBillDetialRepository _processRoute_CraftBillDetialRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public ProcessRoute_CraftBillServices(PowerHubMesContext powerHubMesContext, IProcessRoute_CraftBillRepository iProcessRoute_CraftBillRepository,
            UserServices userServices, IMapper mapper, IProcessRoute_CraftBillDetialRepository processRoute_CraftBillDetialRepository) : base(powerHubMesContext)
        {
            _iProcessRoute_CraftBillRepository = iProcessRoute_CraftBillRepository;
            _userServices = userServices;
            _mapper = mapper;
            _processRoute_CraftBillDetialRepository = processRoute_CraftBillDetialRepository;
        }

        /// <summary>
        /// 获取工艺路线列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProcessRoute_CraftBill>>> GetProcessRoute_CraftBillPageListAsync(QueryProcessRoute_CraftBillDTO query)
        {
            return await _iProcessRoute_CraftBillRepository.GetProcessRoute_CraftBillPageListAsync(query);
        }
        /// <summary>
        /// 工艺路线明细列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProcessRoute_CraftBillDetial>>> GetProcessRoute_CraftBillDetialsPageListAsync(QueryProcessRoute_CraftBillDTO query)
        {
            return await _processRoute_CraftBillDetialRepository.GetProcessRoute_CraftBillDetialPageListAsync(query);
        }

        /// <summary>
        /// 添加工艺路线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProcessRoute_CraftBillAsync(ProcessRoute_CraftBillDTO query)
        {
            ProcessRoute_CraftBill foundation = await GetEntityAsync(d => d.CraftBillName.Equals(query.CraftBillName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的工艺路线名称已存在！");
            }

            string CraftBillId = Guid.NewGuid().ToString();
            query.ProcessRouteCraftBillDetials.ForEach(c =>
            {
                c.Id = Guid.NewGuid().ToString();
                c.CraftBillId = CraftBillId;
                c.Creator = _userServices.UserName;
            });

            //实体转换
            var ProcessRoute_CraftBill = _mapper.Map<ProcessRoute_CraftBill>(query);
            ProcessRoute_CraftBill.Id = CraftBillId;
            ProcessRoute_CraftBill.CraftBillNo = string.IsNullOrEmpty(query.CraftBillNo) || query.CraftBillNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.CraftBillName) : query.CraftBillNo;
            ProcessRoute_CraftBill.Creator = _userServices.UserName;

            return await _iProcessRoute_CraftBillRepository.CreateProcessRoute_CraftBillAsync(ProcessRoute_CraftBill);
        }



        /// <summary>
        /// 批量删除工艺路线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProcessRoute_CraftBillAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iProcessRoute_CraftBillRepository.DeletionProcessRoute_CraftBillAsync(ids);
        }


        /// <summary>
        /// 更新工艺路线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProcessRoute_CraftBillAsync(ProcessRoute_CraftBillDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取工艺路线信息
            ProcessRoute_CraftBill ProcessRoute_CraftBill = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (ProcessRoute_CraftBill == null)
            {
                throw new Exception("更新工艺路线信息发生了异常,请刷新页面后试试");
            }
            ProcessRoute_CraftBill.CraftBillName = query.CraftBillName;
            ProcessRoute_CraftBill.Remark = query.Remark;
            ProcessRoute_CraftBill.Updator = _userServices.UserName;


            return await _iProcessRoute_CraftBillRepository.UpdateProcessRoute_CraftBillAsync(ProcessRoute_CraftBill);

        }

        /// <summary>
        /// 修改明细排序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<OperationResult<bool>> UpdateProcessRoute_CraftBillDetialAsync(List<ProcessRoute_CraftBillDetial> query)
        {
            return await _processRoute_CraftBillDetialRepository.UpdateProcessRoute_CraftBillDetialAsync(query);
        }

        /// <summary>
        /// 删除明细
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<OperationResult<bool>> DeletionProcessRoute_CraftBillDetialAsync(List<string> ids)
        {
            return await _processRoute_CraftBillDetialRepository.DeletionProcessRoute_CraftBillDetialAsync(ids);
        }
    }
}
